﻿using NORM.Entity;
using NORM.SQLObject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NORM.Test
{
    public partial class 迸发测试 : Form
    {
        public 迸发测试()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dd1();
            dd2();
        }

        private void dd1()
        {
            System.Threading.Thread[] ths = {
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler2)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler2)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler)),
                    new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Handler))
                                                };
            for (int i = 0; i < ths.Length; i++)
            {
                ths[i].Start(i);
            }
        }

        private void Handler2(object id)
        {
            try
            {
                //NORM.Logger.LoggerHelper.Debug(id + "开始执行", DateTime.Now.ToString());
                var connectionString = "Server=(Local);Initial Catalog=DevelopFramework_Demo;User ID=sa;Pwd=123456;";
                using (var db = NORM.DataBase.DataBaseFactory.Create(connectionString, DataBase.DataBaseTypes.SqlDataBase))
                {
                    Models.T_Sys_Logger model = new Models.T_Sys_Logger();
                    OQL oql = OQL.From(model)
                        .Where(w => w.Compare(model.Type, "=", 1) && w.Compare(model.Datetime, ">", DateTime.Now.AddDays(-30)))
                        .OrderBy(O => O.Asc(model.Datetime))
                        .Select()
                        .End;


                    model.Type = 4;
                    model.StateCode = "300";
                    OQL oql2 = new OQL(model)
                        .Update(m => m.Set(model.Type) && m.Set(model.StateCode))
                        .Where(w => w.Compare(model.ID, ">", 1661)).End;

                    EntityQuery<Models.T_Sys_Logger>.Instance.Excute(oql2, db);


                    var list = EntityQuery<Models.T_Sys_Logger>.Instance.QueryList(oql, db);
                    NORM.Logger.LoggerHelper.Debug(id + "执行成功", DateTime.Now.ToString());
                }
            }
            catch (Exception ex)
            {
                NORM.Logger.LoggerHelper.Debug(id + "执行中出现错误:" + ex.Message, DateTime.Now.ToString());
            }
        }

        private void Handler(object id)
        {
            try
            {
                //NORM.Logger.LoggerHelper.Debug(id + "开始执行", DateTime.Now.ToString());
                var connectionString = "Server=(Local);Initial Catalog=DevelopFramework_Demo;User ID=sa;Pwd=123456;";
                using (var db = NORM.DataBase.DataBaseFactory.Create(connectionString, DataBase.DataBaseTypes.SqlDataBase))
                {
                    Models.T_Sys_Logger model = new Models.T_Sys_Logger();
                    OQL oql = OQL.From(model)
                        .Where(w => w.Compare(model.Type, "=", 1) && w.Compare(model.Datetime, ">", DateTime.Now.AddDays(-30)))
                        .OrderBy(O => O.Asc(model.Datetime))
                        .Select()
                        .End;


                    model.Type = 4;
                    model.StateCode = "300";
                    OQL oql2 = new OQL(model)
                        .Update(m => m.Set(model.Type) && m.Set(model.StateCode))
                        .Where(w => w.Compare(model.ID, "<", 1661)).End;

                    EntityQuery<Models.T_Sys_Logger>.Instance.Excute(oql2, db);


                    var list = EntityQuery<Models.T_Sys_Logger>.Instance.QueryList(oql, db);
                    NORM.Logger.LoggerHelper.Debug(id + "执行成功", DateTime.Now.ToString());
                }
            }
            catch (Exception ex)
            {
                NORM.Logger.LoggerHelper.Debug(id + "执行中出现错误:" + ex.Message, DateTime.Now.ToString());
            }
        }

        private void dd2()
        {
            var connectionString = "Server=(Local);Initial Catalog=DevelopFramework_Demo;User ID=sa;Pwd=123456;";
            using (var db = NORM.DataBase.DataBaseFactory.Create(connectionString, DataBase.DataBaseTypes.SqlDataBase))
            {
                Models.T_Sys_Logger model = new Models.T_Sys_Logger();
                OQL oql = OQL.From(model).Where(w => w.Compare(model.Type, "=", 4))
                    .OrderBy(o => o.Asc(model.Datetime))
                    .Select(() =>
                    {
                        return new { ID = model.ID, Content = model.Content };
                    })
                    .Limit(3).End;                

                var list = EntityQuery<Models.T_Sys_Logger>.Instance.QueryList(oql, db);
                var data = db.QueryList<Models.T_Sys_Logger>(oql);
            }
           
        }

    }
}
